﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace VTESCore.Utilities
{
	public static class StringUtils
	{
		public static List<string> MostSimilarStrings(string source_string, ICollection<string> comparison_strings)
		{
			Debug.Assert(comparison_strings.Count > 1, "MostSimilarStrings needs a non empty comparison strings list");
			SortedDictionary<int, List<string>> strings_sorted_by_score = new SortedDictionary<int, List<string>>();

			foreach(string comparison_string in comparison_strings)
			{
				int distance = LevenshteinDistance.Compute( source_string.ToLower(), comparison_string.ToLower() );
				if(strings_sorted_by_score.ContainsKey(distance))
				{
					strings_sorted_by_score[distance].Add( comparison_string );
				}
				else
				{
					List<string> new_list = new List<string>();
					new_list.Add( comparison_string );
					strings_sorted_by_score.Add( distance, new_list );
				}
			}
			
			foreach(KeyValuePair<int, List<string>> kvp in strings_sorted_by_score)
			{
				// just return the first list as it will contain the most similar ones
				return kvp.Value;
			}
			// if we got here we must have had an empty comparison_strings list...why would the caller do that? :(
			// oh well, let's return an list with one empty string just so we return something
			List<string> duff_list = new List<string>();
			duff_list.Add( "" );
			return duff_list;
		}
	}
}
